package com.example.shop;

import com.example.shop.order.WXPayProperties;
import com.example.shop.order.config.pay.WXNativePayConfig;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@EnableConfigurationProperties(WXPayProperties.class)
@ComponentScan("com.example.shop.order")
@MapperScan("com.example.shop.order.dao")
//@SpringBootApplication
public class ShopOrderAutoConfiguration {


    @Bean
    public WXPay wxPay(WXPayProperties wxPayProperties) throws Exception {
        WXNativePayConfig nativePayConfig = new WXNativePayConfig();
        nativePayConfig.setAPP_ID(wxPayProperties.getAPP_ID())
                .setMCH_ID(wxPayProperties.getMCH_ID())
                .setKEY(wxPayProperties.getKEY());
        if(wxPayProperties.getCertDataPath()!=null){
            try {
                nativePayConfig.setCertDataPath(wxPayProperties.getCertDataPath());
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("ShopOrderAutoConfiguration里创建NativePayConfig对象的读取证书出错");
            }
        }
        WXPayConfig config = nativePayConfig;
        WXPay wxpay = new WXPay(config);
        return wxpay;
    }
}
